/*
Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package it.kirys.rilego.gui.customclasses;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/**
 * Panel for image showing.
 * @author Federico Improta
 */
public class SimpleImagePanel extends JPanel {

	private String path;
	private boolean scaleImageToPanel = true;
	private BufferedImage img = null;

	/**
	 * Set the panel scaling mode
	 * @param scaleImageToPanel true make the image to be scaled to the panel size
	 * false set that the panel is scaled to the image size.
	 */
	public void setScaleImageToPanel(boolean scaleImageToPanel) {
		this.scaleImageToPanel = scaleImageToPanel;
	}//public void setScaleImageToPanel(boolean scaleImageToPanel)

	/**
	 * Return the panel scaling mode
	 * @return true means the image is scaled to the panel size
	 * false that the panel is scaled to the image size.
	 */
	public boolean isScaleImageToPanel() {
		return scaleImageToPanel;
	}


	/**
	 * Default constructor
	 */
	public SimpleImagePanel() {
		this.path = null;
		img = null;
		scaleImageToPanel = true;
	}//public SimpleImagePanel()

	/**
	 * Constructor
	 * @param scaleImageToPanel true make the image to be scaled to the panel size
	 * false set that the panel is scaled to the image size.
	 */
	public SimpleImagePanel(boolean scaleImageToPanel) {
		this.path = null;
		img = null;
		this.scaleImageToPanel = scaleImageToPanel;
	}//public SimpleImagePanel(boolean scaleImageToPanel)

	/**
	 * Assign an image to the panel
	 * @param image the image to be assigned to the panel
	 */
	public void setImage(BufferedImage image) {
		img = image;
		this.invalidate();
		if ((!scaleImageToPanel) && (img != null)) {
			this.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
		} else {
			this.setPreferredSize(null);
		}
		this.revalidate();
		this.repaint();
	}//public void setImage(BufferedImage image)

	/**
	 * Paint the panel, this will draw the image to the panel
	 * @param g
	 */
	@Override
	public void paint(Graphics g) {
		//draw the image
		if (img != null) {
			int newwidth = 0;
			int newheight = 0;
			int offsetx = 0;
			int offsety = 0;
			{
				//Calcolo delle nuove dimensioni per l'immagine
				float r = 1.0f * img.getWidth() / img.getHeight();
				float s = 1.0f * this.getWidth() / getHeight();
				if (r > s) {
					newwidth = this.getWidth();
					newheight = java.lang.Math.round(this.getWidth() / r);
					offsety = (this.getHeight() - newheight) / 2;
				} else {
					newwidth = java.lang.Math.round(this.getHeight() * r);
					newheight = this.getHeight();
					offsetx = (this.getWidth() - newwidth) / 2;
				}
			}
			g.draw3DRect(0, 0, this.getWidth(), this.getHeight(), true);
			super.paint(g);
			if (scaleImageToPanel) {
				g.drawImage(img, offsetx, offsety, newwidth, newheight, null);
			} else {
				g.drawImage(img, offsetx, offsety, null);
			}

		}
	}//public void paint(Graphics g)
}
